Skip to content

⚡ Optimize Compressor by reusing matches vector#375

Merged
404Setup merged 1 commit intomainfrom
optimize-compressor-matches-reuse-17496430080156893144
Feb 24, 2026
Merged

⚡ Optimize Compressor by reusing matches vector#375
404Setup merged 1 commit intomainfrom
optimize-compressor-matches-reuse-17496430080156893144

Conversation

@404Setup
Copy link
Copy Markdown
Owner

This PR optimizes the memory allocation strategy for the match finding process in the Compressor struct.

💡 What

Instead of allocating a new Vec<(u16, u16)> for every block in calculate_block_size_near_optimal and compress_near_optimal_block, the vector is now a field of the Compressor struct (self.matches). It is allocated once when the Compressor is created (conditionally, only for compression levels >= 10) and reused across all blocks.

🎯 Why

Allocating a vector inside a hot loop (or a function called repeatedly in a loop) is a known performance anti-pattern. Even though the vector starts empty, the repeated allocation and potential reallocation as it grows (although find_matches clears it, the capacity is lost if we drop the vector) adds overhead. By reusing the vector, we amortize the allocation cost over the entire compression session and maintain the capacity, reducing allocator pressure.

📊 Measured Improvement

Benchmark Compress/libdeflate-rs L Level 10 showed a ~5% reduction in execution time and a corresponding increase in throughput.

Baseline:

time:   [163.67 ms 165.99 ms 168.69 ms]
thrpt:  [94.846 MiB/s 96.393 MiB/s 97.755 MiB/s]

Optimized:

time:   [155.61 ms 157.48 ms 159.92 ms]
thrpt:  [100.05 MiB/s 101.60 MiB/s 102.82 MiB/s]

This confirms the effectiveness of the optimization.


PR created automatically by Jules for task 17496430080156893144 started by @404Setup

Moved the allocation of the `matches` vector from `calculate_block_size_near_optimal` and `compress_near_optimal_block` to the `Compressor` struct. This avoids repeated allocations for every block processed, improving Level 10 compression performance by approximately 5%.

- Added `matches: Vec<(u16, u16)>` to `Compressor` struct.
- Initialized `matches` in `Compressor::new`.
- Reused `self.matches` in `calculate_block_size_near_optimal` and `compress_near_optimal_block`.

Co-authored-by: 404Setup <[email protected]>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@404Setup 404Setup merged commit a9034cb into main Feb 24, 2026
1 check passed
@404Setup 404Setup deleted the optimize-compressor-matches-reuse-17496430080156893144 branch February 24, 2026 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant